#------------------------------------------------------------------------------
# Project:     Multi-Programmer Game Demo
# Name:        game.py
# Purpose:     Setup and Main Loop
# 
# Authors:     Chris Spooner, Chris La-Roo
# Main Author: Chris Spooner
#
# Created:     11/30/2012
# Copyright:   (c) Chris Spooner 2012
# Licence:     New BSD
#------------------------------------------------------------------------------
import pygame, math, sys, random
from Ball import Ball
from Paddle import Paddle
from Menu import Button

def ai(player, ball):
    if player.rect.center[1] < ball.rect.center[1]:
        player.direction("down")
    elif player.rect.center[1] > ball.rect.center[1]:
        player.direction("up")
    else:
        player.direction("stop up")
        

#stole stuff from run.py in Ion Driver
if pygame.mixer:
    pygame.mixer.init()

pygame.init()

clock = pygame.time.Clock()

screenWidth = 800
screenHeight = 600

singleplayer = Button("SINGLEPLAYER", [0,10], (20, 100, 20))
exit = Button("EXIT", [0,10], (20, 100, 20))
singleplayer.place([300, 300])
exit.place([300, 400])

screenSize = screenWidth, screenHeight
screen = pygame.display.set_mode(screenSize)


    
player = Paddle(["rsc/paddle/paddle_1.png"], 10, screenSize)
player2 = Paddle(["rsc/paddle/paddle_1.png"], 5, screenSize)
player.place([15, 400])
player2.place([785, 400])
balls = []
maxBalls = 1


run = False

bgColor = 0,0,0
while True:
    while not run:
        for event in pygame.event.get():
            if event.type == pygame.QUIT: sys.exit()
            elif event.type == pygame.KEYDOWN:
                if (event.key == pygame.K_UP or event.key == pygame.K_w) and not singleplayer.highlighted:
                    singleplayer.highlighted = True
                    exit.highlighted = False
                elif event.key == pygame.K_DOWN or event.key == pygame.K_s and not exit.highlighted:
                    exit.highlighted = True
                    singleplayer.highlighted = False
                elif event.key == pygame.K_SPACE and exit.highlighted == True:
                    sys.exit()
                elif event.key == pygame.K_SPACE and singleplayer.highlighted == True:
                    run = True
            elif event.type == pygame.MOUSEMOTION:
                if singleplayer.collidePt(event.pos):
                    singleplayer.highlighted = True
                    exit.highlighted = False
                elif exit.collidePt(event.pos):
                    singleplayer.highlighted = False
                    exit.highlighted = True
            elif event.type == pygame.MOUSEBUTTONDOWN:
                if event.button == 1:
                    if singleplayer.collidePt(event.pos):
                        singleplayer.clicked = True
                        run = True
                    elif exit.collidePt(event.pos):
                        exit.clicked = True
                        sys.exit()

        singleplayer.update((10, 200, 10))
        exit.update((10, 200, 10))
        
        screen.fill(bgColor)
        # screen.blit(background.surface, background.rect)
        screen.blit(singleplayer.surface, singleplayer.rect)
        screen.blit(exit.surface, exit.rect)
        pygame.display.flip()
        
        while run:
            for event in pygame.event.get():
                if event.type == pygame.QUIT: sys.exit()
                elif event.type == pygame.KEYDOWN:
                    if event.key == pygame.K_w or event.key == pygame.K_UP:
                        player.direction("up")
                    elif event.key == pygame.K_s or event.key == pygame.K_DOWN:
                        player.direction("down")
                elif event.type == pygame.KEYUP:
                    if (event.key == pygame.K_UP 
                        or event.key == pygame.K_w):
                            player.direction("stop up")
                    elif (event.key == pygame.K_DOWN 
                        or event.key == pygame.K_s):
                            player.direction("stop down")
            
            #Create Balls
            while len(balls) < maxBalls:
                balls += [Ball(["rsc/ball/puck.png", "rsc/paddle/paddle1.png", "rsc/paddle/paddle2.png", "rsc/paddle/paddle3.png", "rsc/paddle/paddle4.png"], [9, 9], screenSize)]
                for ball in balls:
                    ball.place([400, 300])
            #Move
            player.move()
            player2.move()
            player.wallCollide()
            player2.wallCollide()
            # player.animate()
            for ball in balls:
                ball.move()
                

            #Collisions With Walls, and Paddle
            player.wallCollide()
            for ball in balls:
                ai(player2, ball)
                ball.collideFloor()
                ball.collideWall()
                ball.animate()
                player.ballCollide(ball)
                player2.ballCollide(ball)


            #Remove Non-Living Balls
            for ball in balls:
                if not ball.living:
                    balls.remove(ball)

            #Draw Everything to Screen
            screen.fill(bgColor)
            screen.blit(player.surface, player.rect)
            screen.blit(player2.surface, player2.rect)
            for ball in balls:
                screen.blit(ball.surface, ball.rect)
            pygame.display.flip()    
            clock.tick(30)
            print clock.get_fps()

     
    